<!--
 * @Author: Frane
 * @Date: 2020-06-28 14:09:11
 * @LastEditors: 範遠茂
 * @LastEditTime: 2020-07-06 15:31:33
 * @Description: 添加收货地址
--> 
<template>
  <div>
    <van-nav-bar title="添加收件地址" left-text="返回" left-arrow @click-left="goback" />
    <van-address-edit
      :area-list="areaList"
      :address-info="AddressInfo"
      show-search-result
      show-set-default
      :search-result="searchResult"
      save-button-text="确认添加"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
// import { Toast } from "vant";
import * as api from "@/api/apiList/user/address/address.js";
import area1 from "../area.js";
export default {
  data() {
    return {
      areaList: area1,//省市区数据
      searchResult: [],//详细地址搜索结果
      AddressInfo: {},//收货人信息初始数据
    };
  },
  methods: {
    async API_addAddress(//添加收货地址
      consignee,//收货人姓名
      consignee_mobile,//电话
      consignee_address,//详细地址
      province_name,//省
      city_name,//市
      area_name,//区
      is_default ,//是否默认
      country_id = 1,//国家id
      province_id = 6,//省id
      city_id = 76,//市id
      area_id = 696,//地区id
      country_name = "中国"//国家
    ) {
      let data = {
        consignee,
        consignee_mobile,
        consignee_address,
        province_name,
        city_name,
        area_name,
        is_default,
        country_id,
        province_id,
        city_id,
        area_id,
        country_name
      };

      return await api.post_Address(this.qs.stringify(data));
    },

    goback() {
      //返回上一页
      this.$router.go(-1);
    },
    onSave(content) {
      //保存添加的收货信息

      if (content.isDefault === true) {
        content.isDefault = "1";
      } else {
        content.isDefault = "0";
      }
      let res = this.API_addAddress(
        content.name,
        content.tel,
        content.addressDetail,
        content.province,
        content.city,
        content.county,
        content.isDefault
      );
      console.log(res)
      this.$toast("添加成功");
      this.$router.go(-1)
    },
    
    onChangeDetail(val) {//详细地址搜索
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区"
          }
        ];
      } else {
        this.searchResult = [];
      }
    }
  }
};
</script>

<style>
</style>